package com.lovi.user.controller;

import com.lovi.param.UserCheckParams;
import com.lovi.param.UserLoginParams;
import com.lovi.pojo.User;
import com.lovi.user.service.serviceImpl.UserServiceImpl;
import com.lovi.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserServiceImpl userService;


    /**
     * 账号检查
     * @param checkParams
     * @param bindingResult 查看校验结果
     */
    @PostMapping("/check")
    public R checkAccount(@RequestBody @Validated UserCheckParams checkParams, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            log.info("UserController.checkAccount 结果为->{}", "错误！账号不存在！");
            return R.fail("错误！账号不存在！");
        }
        //注册检查账号
        return userService.checkUserAccount(checkParams);

    }

    /**
     * 注册方法，需要对密码加盐存入数据库
     * @param user
     * @param result
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody @Validated User user,BindingResult result){
        // 参数校验
        if(result.hasErrors()){
            log.info("UserController.register 结果为->{}", "参数校验异常");
            return R.fail("参数校验异常");
        }
        // 实现注册逻辑
        return userService.register(user);
    }

    /**
     * 登录实现
     * @param userLoginParams
     * @param result
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody @Validated UserLoginParams userLoginParams, BindingResult result){
        // 参数校验
        if(result.hasErrors()){
            log.info("UserController.login 结果为->{}", "参数校验异常");

            return R.fail("参数校验异常",userLoginParams);
        }
        // 实现登录逻辑
        return userService.login(userLoginParams);
    }





}
